﻿using System;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Web;
using CH.FourBoards.LagerWalti.FrontEnd.View;
using CH.FourBoards.LagerWalti.FrontEnd.View.Controls;
using CH.FourBoards.LagerWalti.FrontEnd.View.Util;

namespace CH.FourBoards.LagerWalti.WebFrontEnd.View.Master
{
	public class PageBase : SuperPageBase
	{
		private readonly IncludeContainer _includes = new IncludeContainer();

		protected override string ProjectRootNamespace
		{
            get { return "CH.FourBoards.LagerWalti.FrontEnd"; }
		}

		protected bool IsCookieDisabled()
		{
			var currentUrl = Request.RawUrl;

			if (Request.QueryString["cookieCheck"] == null) {
				try {
					var c = new HttpCookie("SupportCookies", "true");
					Response.Cookies.Add(c);

					if (currentUrl.Contains("?")) {
						currentUrl = currentUrl + "&cookieCheck=true";
					} else {
						currentUrl = currentUrl + "?cookieCheck=true";
					}
					Response.Redirect(currentUrl);
				} catch {
				}
			}

			return (!Request.Browser.Cookies || Request.Cookies["SupportCookies"] == null);

		}

		public void ShowException(Exception ex)
		{
			var master = Page.Master as Main;
			if (master == null) {
				throw new ArgumentException("Page has invalid Master");
			}

			var error = "<strong>Exception occured:</strong><br><br>" + new ContentRenderer().Format(ex);
			master.ShowError(error);
		}

		protected void IncludeCss(string relativePath)
		{
			IncludeCss(relativePath, false);
		}

		protected string ProfileName
		{
			get
			{
				return ConfigurationManager.AppSettings["ProfileName"];
			}
		}

		protected string VersionName
		{
			get
			{
				return ConfigurationManager.AppSettings["VersionName"];
			}
		}

		protected string ReleaseDate
		{
			get
			{
				var date = new FileInfo(Assembly.GetExecutingAssembly().Location).LastWriteTime;
				return new ContentRenderer().ToDateOnly(date);
			}
		}
        
		protected void IncludeCss(string relativePath, bool external)
		{
			var root = external ? string.Empty : ApplicationRoot;
			_includes.AddStyle(root + relativePath);
		}

		protected void IncludeScript(string relativePath)
		{
			IncludeScript(relativePath, false);
		}

		protected void IncludeScript(string relativePath, bool external)
		{
			var root = external ? string.Empty : ApplicationRoot;
			_includes.AddScripts(root + relativePath);
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			IncludeScript("View/Master/jquery-1.10.2.min.js");
			IncludeScript("View/Master/extensions.js");
			IncludeScript("View/Master/common.js");

			IncludeCss("View/Master/bootstrap/css/bootstrap.min.css");
			IncludeScript("View/Master/bootstrap/js/bootstrap.min.js");
			IncludeCss("View/Master/style.css");
		}

		protected T InstallControl<T>(string path) where T : ControlBase
		{
			var ret = (T) LoadControl(path);
			ret.Includes = _includes;
			return ret;
		}

		protected String VirtualFolderPath
		{
			get
			{
				var ret = Path.GetDirectoryName(AppRelativeVirtualPath);
				ret = ret ?? string.Empty;
				ret = ret.Replace("~" + Path.DirectorySeparatorChar, string.Empty);
				ret = ret.Replace(Path.DirectorySeparatorChar, '/');
				if (ret.StartsWith("/")) {
					ret = ret.Substring(1);
				}
				return ret + '/';
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			_includes.Render(Page.Header.Controls);
		}

	}
}